<script>
export default {
  name: "MyItem",
  props: {
    todoObj: Object
  },
  methods: {
    //选中当前的任务
    checkTodo(id){
      this.$bus.$emit("checkTodo", id)
    },
    //删除当前任务
    deleteTodo(id){
      this.$bus.$emit("deleteTodo",id)
    }
  },

  beforeDestroy() {
    // 只移除当前组件实例的监听器，而不是所有同名监听器
    this.$bus.$off("checkTodo", this.checkTodo);
    this.$bus.$off("deleteTodo", this.deleteTodo);
  }
}
</script>

<template>
  <li>
    <label>
      <input type="checkbox" :checked="todoObj.done" @click="checkTodo(todoObj.id)"/>
      <span>{{ todoObj.content }}</span>
    </label>
    <button class="btn btn-danger" @click="deleteTodo(todoObj.id)">删除</button>
  </li>
</template>

<style scoped>
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
li:hover {
  background: #aaa;
}

li:hover button{
  display: block;
}
</style>